home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / cml-098.lha / cml-0.9.8 / library / future.sml < prev    next >
Encoding:
Text File  |  1991-06-16  |  948 b   |  36 lines

  1. (* future.sml
  2.  *
  3.  * COPYRIGHT (c) 1990 by John H. Reppy.  See COPYRIGHT file for details.
  4.  *
  5.  * This is an implementation of a multi-lisp style future operation.
  6.  *)
  7.  
  8. signature FUTURE =
  9.   sig
  10.     structure CML : CONCUR_ML
  11.     val future : ('a -> '2b) -> 'a -> '2b CML.event
  12.   end (* FUTURE *)
  13.  
  14. functor Future (CML : CONCUR_ML) =
  15.   struct
  16.  
  17.     structure CML = CML
  18.  
  19.   (* future : ('a -> '2b) -> 'a -> '2b event
  20.    * Spawn a thread to evaluate f applied to x and return an event that will
  21.    * supply the result.  If f raises an exception, then synchronizing on the
  22.    * future event will raise the same exception.
  23.    *)
  24.     fun future f x = let
  25.       datatype 'a msg_t = RESULT of 'a | EXN of exn
  26.       val resV = CML.condVar()
  27.       fun doit () = CML.writeVar (resV, (RESULT(f x) handle ex => EXN ex))
  28.       in
  29.         CML.spawn doit;
  30.         CML.wrap (
  31.           CML.readVarEvt resV,
  32.           fn (RESULT x) => x | (EXN ex) => raise ex)
  33.       end
  34.  
  35.   end (* functor Future *)
  36.